using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class GameManager : MonoBehaviour
{
    [Header("游戏设置")]
    [SerializeField] private int startingLives = 3;
    [SerializeField] private int currentScore = 0;
    [SerializeField] private int comboMultiplier = 1;
    [SerializeField] private int comboThreshold = 5; // 每累积多少连击增加一次倍率
    
    [Header("UI引用")]
    [SerializeField] private Text scoreText;
    [SerializeField] private Text comboText;
    [SerializeField] private Text livesText;
    [SerializeField] private GameObject gameOverPanel;
    
    [Header("音频设置")]
    [SerializeField] private AudioClip hitSound;
    [SerializeField] private AudioClip missSound;
    [SerializeField] private AudioClip gameOverSound;
    [SerializeField] private AudioSource musicSource;
    [SerializeField] private AudioSource sfxSource;
    
    // 游戏状态
    private int currentLives;
    private int currentCombo;
    private bool isGameOver = false;
    
    // 单例实例
    public static GameManager Instance { get; private set; }
    
    private void Awake()
    {
        // 单例模式
        if (Instance == null)
        {
            Instance = this;
        }
        else if (Instance != this)
        {
            Destroy(gameObject);
            return;
        }
        
        // 初始化游戏状态
        currentLives = startingLives;
        currentScore = 0;
        currentCombo = 0;
        isGameOver = false;
        
        // 确保游戏结束面板初始隐藏
        if (gameOverPanel != null)
        {
            gameOverPanel.SetActive(false);
        }
    }
    
    private void OnEnable()
    {
        // 订阅音符事件
        Note.OnNoteHit += HandleNoteHit;
        Note.OnNoteMissed += HandleNoteMissed;
    }
    
    private void OnDisable()
    {
        // 取消订阅音符事件
        Note.OnNoteHit -= HandleNoteHit;
        Note.OnNoteMissed -= HandleNoteMissed;
    }
    
    private void Start()
    {
        UpdateUI();
        StartMusic();
    }
    
    // 处理音符命中
    private void HandleNoteHit(int baseScore, bool isPerfect)
    {
        if (isGameOver) return;
        
        // 增加连击数
        currentCombo++;
        
        // 计算倍率
        comboMultiplier = 1 + (currentCombo / comboThreshold);
        
        // 计算最终得分
        int finalScore = baseScore * comboMultiplier;
        if (isPerfect)
        {
            finalScore = (int)(finalScore * 1.5f); // 完美命中额外加分
        }
        
        // 更新总分
        currentScore += finalScore;
        
        // 更新UI
        UpdateUI();
    }
    
    // 处理音符错过
    private void HandleNoteMissed()
    {
        if (isGameOver) return;
        
        // 重置连击
        currentCombo = 0;
        comboMultiplier = 1;
        
        // 减少生命值
        currentLives--;
        
        // 播放错过音效
        PlayMissSound();
        
        // 更新UI
        UpdateUI();
        
        // 检查游戏是否结束
        if (currentLives <= 0)
        {
            GameOver();
        }
    }
    
    // 更新UI显示
    private void UpdateUI()
    {
        if (scoreText != null)
        {
            scoreText.text = "分数: " + currentScore.ToString();
        }
        
        if (comboText != null)
        {
            comboText.text = "连击: " + currentCombo.ToString() + " x" + comboMultiplier.ToString();
        }
        
        if (livesText != null)
        {
            livesText.text = "生命: " + currentLives.ToString();
        }
    }
    
    // 游戏结束
    private void GameOver()
    {
        isGameOver = true;
        
        // 停止音乐
        if (musicSource != null && musicSource.isPlaying)
        {
            musicSource.Stop();
        }
        
        // 播放游戏结束音效
        if (sfxSource != null && gameOverSound != null)
        {
            sfxSource.PlayOneShot(gameOverSound);
        }
        
        // 显示游戏结束面板
        if (gameOverPanel != null)
        {
            gameOverPanel.SetActive(true);
        }
        
        // 停止生成音符
        NoteSpawner spawner = FindObjectOfType<NoteSpawner>();
        if (spawner != null)
        {
            spawner.StopSpawning();
        }
    }
    
    // 重新开始游戏
    public void RestartGame()
    {
        // 重新加载当前场景
        UnityEngine.SceneManagement.SceneManager.LoadScene(
            UnityEngine.SceneManagement.SceneManager.GetActiveScene().buildIndex);
    }
    
    // 返回主菜单
    public void ReturnToMainMenu()
    {
        // 加载主菜单场景
        UnityEngine.SceneManagement.SceneManager.LoadScene("MainMenu");
    }
    
    // 开始播放音乐
    public void StartMusic()
    {
        if (musicSource != null && !musicSource.isPlaying)
        {
            musicSource.Play();
        }
    }
    
    // 播放命中音效
    public void PlayHitSound()
    {
        if (sfxSource != null && hitSound != null)
        {
            sfxSource.PlayOneShot(hitSound);
        }
    }
    
    // 播放错过音效
    public void PlayMissSound()
    {
        if (sfxSource != null && missSound != null)
        {
            sfxSource.PlayOneShot(missSound);
        }
    }
    
    // 获取当前游戏状态
    public bool IsGameOver()
    {
        return isGameOver;
    }
    
    // 获取当前音乐播放时间
    public float GetMusicTime()
    {
        return musicSource != null ? musicSource.time : 0f;
    }
}